<template>
	<view class="container" @click="save">
		<tki-qrcode v-if="ifShow" cid="qrcode1" ref="qrcode" :val="val" :size="size" :unit="unit" :background="background" :foreground="foreground" :pdground="pdground" :icon="icon" :iconSize="iconsize" :lv="lv" :onval="onval" :loadMake="loadMake" :usingComponents="true" @result="qrR" />
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
import ayQrcode from "@/components/ay-qrcode/ay-qrcode.vue"
	import tkiQrcode from '@/components/tki-qrcode/tki-qrcode.vue'
	export default {

		data() {
			return {
				userinfo:'',
				modal_qr: false,
				url:'' ,
				ifShow: true,
				val: '', // 要生成的二维码值
				size: 300, // 二维码大小
				unit: 'upx', // 单位
				background: 'black', // 背景色
				foreground: 'white', // 前景色
				pdground: '#32dbc6', // 角标色
				icon: '../../static/64.jpg', // 二维码图标
				iconsize: 50, // 二维码图标大小
				lv: 3, // 二维码容错级别 ， 一般不用设置，默认就行
				onval: false, // val值变化时自动重新生成二维码
				loadMake: true, // 组件加载完成后自动生成二维码
				src: '' // 二维码生成后的图片地址或base64
			}
		},
		onLoad() {
			this.getUserUrls();
		},
		onShow() {
			this. val="http://pth5.njkw668.com/#/pages/user/user?&pid=";
		},
onReady() {
		            let that = this;
		           this.creatQrcode()
		        },
		computed: {
			...mapState(['hasLogin'])
		},
		methods: {
			code() {
				uni.downloadFile({
					url:this.url,
					success: (res) => { 
						console.log(res.tempFilePath)
						// 获取到图片本地地址后再保存图片到相册(因为此方法不支持远程地址)
						uni.saveImageToPhotosAlbum({
							filePath: res.tempFilePath,
							success: () => {
								uni.showToast({
									title: "保存成功！"
								})
							},
							fail: (err) => {
								console.log(err)
							}
						})
					}
				})
			},
			
			getUserUrls() {
				uni.getStorage({
					key: 'userinfo',
					success: (res) => {
						this.userinfo = res.data;
						this.extensionImg = "http://42.192.120.95:2000/index/index/user_url?user_id=" + this
							.userinfo.member_id;
						// /con
						//console.log(this.userinfo, '用户信息')
						if (this.userinfo == '') {
							console.log(userInfo, "00000");
							uni.navigateTo({
								url: `/pages/login/login`
							})
						}

					},
					fail: (res) => {
						uni.navigateTo({
							url: `/pages/login/login`
						})
					}
				});
				// uni.request({
				// 	url:"http://42.192.120.95:2000/index/index/user_url",
				// 	method:"POST",
				// 	data:{
				// 		"user_id":this.userinfo.member_id
				// 	},
				// 	dataType: 'json',  
				// 	 header: {  
				// 	         'content-type': 'application/x-www-form-urlencoded;application/json;charset=UTF-8',  

				// 	          },  
				// 	success:(res)=>{
				// 		// console.log(res)

				// 			this.extensionImg = res.data;

				// 		//resolve(res.data)
				// 	},
				// 	fail:(err)=> {
				// 		uni.showToast({
				// 			title:'请求接口失败'
				// 		})
				// 		reject(err)

				// 	}

				// })
				// this.$http.getUserUrl().then(res => {
				// 	console.log(res, 'sddd')
				// 	if (res.code == 200) {
				// 		this.extensionImg = res.centons.url;
				// 	} else {
				// 		this.$utils.msg(res.centons);
				// 		return false;
				// 	}
				// }).catch(err => {
				// 	this.$utils.msg(err.centons);
				// });
			},
			save() {
				uni.showActionSheet({
					itemList: ["保存到相册"],
					success: res => {
						if (res.tapIndex == 0) {
							uni.downloadFile({
								url: this.extensionImg,
								success: (res) => {
									if (res.statusCode === 200) {
										uni.saveImageToPhotosAlbum({
											filePath: res.tempFilePath,
											success: res => {
												uni.showToast({
													title: "保存成功！"
												})
											}
										})
									}
								}
							});
						}
					}
				})
			},
		}
	}
</script>

<style lang='scss'>
	.container {
		width: 100%;
		height: 94vh;
		background-image: url(../../static/sharebackground.png);
		background-repeat: no-repeat;
		background-size: 100% 100%;

		.extension_erweima {
			width: 350upx;
			height: 350upx;
			position: relative;
			top: 280upx;
			left: 200upx;
		}
	}
</style>
